package ControlLayer;
import java.sql.SQLException;

import ModelLayer.*;
import DBLayer.*;
public class CtrOrderLine {
	public Product searchProduct(int productID) throws SQLException, Exception{
		IFDBProduct dbProduct = new DBProduct();
		Product productObj = new Product();
		productObj.setProductID(productID);
		try{
			productObj = dbProduct.findProduct(productID);
		}catch(SQLException sql){
			sql.printStackTrace();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return productObj;
	}
	public Product searchProduct(String productName)throws SQLException, Exception{
		IFDBProduct dbProduct = new DBProduct();
		Product productObj = new Product();
		productObj.setPName(productName);
		try{
			productObj = dbProduct.searchProductPname(productName);
		}catch(SQLException sql){
			sql.printStackTrace();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return productObj;
	}
	public void addProduct(int saleID, String pName, int quantity){
		IFDBProduct dbProduct = new DBProduct();
		IFDBSalesProducts dbsalesProduct = new DBSalesProducts();
		SalesProducts salesProductObj = new SalesProducts();
		salesProductObj.setSaleID(saleID);
		salesProductObj.setQuantity(quantity);
		try{
			salesProductObj.setProduct(dbProduct.searchProductPname(pName));
		}catch(SQLException sql){
			sql.printStackTrace();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		try{
			dbsalesProduct.addSalesProduct(salesProductObj);
		}catch(SQLException ex){
			System.out.println("Exception in ctrOrderLine addProduct SQLEXCEPTION: "+ex);
			ex.printStackTrace();
		}catch(Exception e){
			System.out.println("Exception in ctrOrderLine addProduct EXCEPTION: "+e);
			e.printStackTrace();
		}
	}
	public void updateOrderLines(SalesProducts salesProduct){
		IFDBSalesProducts dbSalesProduct = new DBSalesProducts();
		try{
			dbSalesProduct.updateProduct(salesProduct.getProduct(),salesProduct.getQuantity(), salesProduct.getSaleID());
		}catch(SQLException sql){
			System.out.println("Exception in ctrOrderLine updateProduct SQLEXCEPTION: "+sql);
			sql.printStackTrace();
		}catch(Exception ex){
			System.out.println("Exception in ctrOrderLine updateProduct EXCEPTION: "+ex);
			ex.printStackTrace();
		}
	}
}